//
//  VideoFileModel.m
//  TransPas
//
//  Created by Liuyujie on 2024/3/27.
//

#import "VideoFileModel.h"

@implementation VideoFileModel

- (void)beginAction
{
    NSFileManager *fileManager = [NSFileManager defaultManager];

    //NSString *userName = NSHomeDirectory();

    /// /Users/用户名/Downloads 下载文件夹
    NSString *root = NSSearchPathForDirectoriesInDomains(NSDownloadsDirectory, NSUserDomainMask, YES).firstObject;
    NSString *end = [NSString stringWithFormat:@"%@/%@", root, @"AAVideoEnd"];
    NSString *name = @"h间l.EP";

    NSArray *mp4FileArray = [fileManager contentsOfDirectoryAtPath:root error:nil];

    for (NSString *dir in mp4FileArray) {
        if ([dir containsString:name]) {
            NSArray *videoArray = [fileManager subpathsAtPath:[NSString stringWithFormat:@"%@/%@", root, dir]];

            for (NSString *mp4Name in videoArray) {
                if ([mp4Name.lowercaseString containsString:@".mp4"]) {
                    NSURL *url1 = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@/%@", root, dir, mp4Name]];
                    NSURL *url2 = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", end, mp4Name]];
                    [fileManager moveItemAtURL:url1 toURL:url2 error:nil];
                }
            }
        }
    }

    NSLog(@"执行完成");
    NSString *cmd = [NSString stringWithFormat:@"open %@", end];
    [[NSTask launchedTaskWithLaunchPath:@"/bin/sh" arguments:@[@"-c", cmd]] waitUntilExit];
}

@end
